package com.shanghaionstar.remote.manager;

import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.enumtype.RemoteCommandsType;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.vehicle.entity.AdditionalDestinationInfo;
import com.onstar.cn.vehicle.entity.DestinationAddress;
import com.onstar.cn.vehicle.entity.DestinationLocation;
import com.onstar.cn.vehicle.entity.VehicleAlert;
import com.onstar.cn.vehicle.entity.VehicleCommand;
import com.onstar.cn.vehicle.entity.VehicleDiagnosticItem;
import com.onstar.cn.vehicle.entity.VehicleLockDoor;
import com.onstar.cn.vehicle.entity.VehicleNAV;
import com.onstar.cn.vehicle.entity.VehicleTBT;
import com.onstar.cn.vehicle.entity.VehicleUnLockDoor;
import com.onstar.cn.vehicle.entity.VehicleWifiInfo;
import com.shanghaionstar.discover.model.WifiInfoModel;
import com.shanghaionstar.remote.model.RemoteModel;
import com.shanghaionstar.utils.ApplicationUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by onstarios on 2017/7/17.
 */

public class RemoteOperateHolder {


    public String getBody(RemoteModel remoteModel) {
        VehicleCommand vehicleCommand = new VehicleCommand();
        setUpVehicleCommand(vehicleCommand, remoteModel);
        return GsonHelper.toJson(vehicleCommand);
    }

    public String getUrl(RemoteCommandsType type) {
        String vin  = ApplicationUtil.getInstance().getVin();
        String commonUrl = "/vehicles/%1$s/commands/%2$s";
        switch (type) {
            case COMMAND_GETHOTSPOTINFO:
            case COMMAND_SETHOTSPOTINFO:
                commonUrl = String.format(commonUrl, vin + "/hotspot", type.value());
                break;
            case COMMANDS_NAV_DESTINATION:
                commonUrl = String.format(commonUrl, vin + "/navUnit", type.value());
                break;
            default:
                commonUrl = String.format(commonUrl, vin, type.value());
                break;
        }
        return VehicleService.PATH + commonUrl;
    }


    private void setUpVehicleCommand(VehicleCommand vehicleCommand, RemoteModel remoteModel) {
        RemoteCommandsType commandType = remoteModel.getRemoteCommandsType();
        switch (commandType) {
            case COMMANDS_LOCK_DOOR:
                lock(vehicleCommand);
                break;
            case COMMANDS_UNLOCK_DOOR:
                unlock(vehicleCommand);
                break;
            case COMMANDS_ALERT:
                alert(remoteModel.isHorn(), remoteModel.isFlash(), vehicleCommand);
                break;
            case COMMANDS_DIAGNOSTICS:
                diagnostics(vehicleCommand);
                break;
            case COMMANDS_TBT_ROUTE:
                tbtRoute(vehicleCommand, remoteModel.poiItemInfo);
                break;
            case COMMANDS_NAV_DESTINATION:
                odd(vehicleCommand, remoteModel.poiItemInfo);
                break;
            case COMMAND_SETHOTSPOTINFO:
                hotSpot(vehicleCommand, remoteModel.model);
                break;
        }
    }

    private void tbtRoute(VehicleCommand vehicleCommand, PoiItemInfo poiItemInfo) {
        VehicleTBT tbt = new VehicleTBT();
        DestinationLocation destinationLocation = new DestinationLocation();
        destinationLocation.setLat(String.valueOf(poiItemInfo.getLatitude()));
        destinationLocation.setLon(String.valueOf(poiItemInfo.getLongitude()));
        tbt.setDestinationLocation(destinationLocation);
        vehicleCommand.setTbtDestination(tbt);
    }


    private void diagnostics(VehicleCommand vehicleCommand) {
        VehicleDiagnosticItem diagnosticItem = new VehicleDiagnosticItem();
        diagnosticItem.setDiagnosticItem(ApplicationUtil.getInstance().getSupportedDiagnostic());
        String[] supportDig = ApplicationUtil.getInstance().getSupportedDiagnostic();
        vehicleCommand.setDiagnosticsRequest(diagnosticItem);
    }

    private void hotSpot(VehicleCommand vehicleCommand, WifiInfoModel model) {
        VehicleWifiInfo wifiInfo = new VehicleWifiInfo();
        wifiInfo.setPassphrase(model.getPassphrase());
        wifiInfo.setSsid(model.getSsid());
        vehicleCommand.setHotspotInfo(wifiInfo);
    }

    private void odd(VehicleCommand vehicleCommand, PoiItemInfo poiItemInfo) {
        VehicleNAV tbt = new VehicleNAV();
        DestinationLocation destinationLocation = new DestinationLocation();
        destinationLocation.setLat(String.valueOf(poiItemInfo.getLatitude()));
        destinationLocation.setLon(String.valueOf(poiItemInfo.getLongitude()));
        tbt.setDestinationLocation(destinationLocation);
        //
        AdditionalDestinationInfo additionalDestinationInfo = new AdditionalDestinationInfo();
        DestinationAddress destinationAddress = new DestinationAddress();

        destinationAddress.setState("China");
        additionalDestinationInfo.setDestinationAddress(destinationAddress);

        tbt.setAdditionalDestinationInfo(additionalDestinationInfo);

        vehicleCommand.setNavDestination(tbt);
    }


    private void lock(VehicleCommand vehicleCommand) {
        VehicleLockDoor lockDoor = new VehicleLockDoor();
        lockDoor.setDelay("0");
        vehicleCommand.setLockDoorRequest(lockDoor);
    }

    private void unlock(VehicleCommand vehicleCommand) {
        VehicleUnLockDoor unlockDoor = new VehicleUnLockDoor();
        unlockDoor.setDelay("0");
        vehicleCommand.setUnlockDoorRequest(unlockDoor);
    }

    private void alert(boolean isHorn, boolean isFlash, VehicleCommand vehicleCommand) {
        String[] array = new String[]{};
        List<String> list = new ArrayList<String>();
        if (isHorn) {
            list.add("Honk");
        }
        if (isFlash) {
            list.add("Flash");
        }
        VehicleAlert alert = new VehicleAlert();
        alert.setDelay("0");
        //		alert.setDuration("60");
        //IDT1 个例只能为30
        alert.setDuration((ApplicationUtil.getInstance().getAlertDuration() != -1 ? ApplicationUtil.getInstance().getAlertDuration() : ApplicationUtil.ALERT_DURATION_DEF) + "");
        alert.setAction(list.toArray(array));
        alert.setOverride(new String[]{"DoorOpen", "IgnitionOn"});
        vehicleCommand.setAlertRequest(alert);
    }
}
